//
//  SinaHomeViewController.swift
//  weibo
//
//  Created by sirius on 15/12/30.
//  Copyright © 2015年 sirius. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD



let SinaHomeReuserIdentifie  = "SinaHomeReuserIdentifier"
class SinaHomeViewController: SinaBaseTableViewController {
    //定义一个可变的数组，完成初始化
//    lazy var list: [SinaStatusModel] = [SinaStatusModel]()
    lazy var viewModel: SinaStatusListViewModel = SinaStatusListViewModel  ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin{
            
      visitor?.setupInfo("关注一些人，回这里看看有什么惊喜",image: nil)
            
        }else{
        //获取首页数据
        getData()
        //注册
        tableView.registerClass(SinaHomeTableViewCell.self, forCellReuseIdentifier: SinaHomeReuserIdentifie)
            //设置预估
            tableView.estimatedRowHeight = 200
            tableView.rowHeight = UITableViewAutomaticDimension
             //ios 6.0 之后添加  下拉刷新
            /*
            1.0 默认的构造函数是init 
            2.0 默认宽高度
            3.0 实例化后添加至tabelView,不实例化无法显示
            4.0 UIControlEventValueChanged 
        */
            refreshControl = SinaRefreshControl()
            refreshControl?.addTarget(self, action: "reloadData", forControlEvents: UIControlEvents.ValueChanged)
        }
    
    }
    func showCount(count: Int){
        /*分析更新提示 nagagationControl.view
        */
        let labelHeight:CGFloat = 44
        let label = UILabel()
        label.frame = CGRectMake(0, 64-labelHeight, kUIScreenWidth, labelHeight)
        label.backgroundColor = UIColor.redColor()
        if count == 0{
            label.text = "暂无更新"
        }else{
            label.text = "更新了\(count)条新鲜事"
        }
        label.textAlignment = NSTextAlignment.Center
        //添加进入
        navigationController?.view.insertSubview(label, belowSubview: (navigationController!.navigationBar))
        //动画
         UIView.animateWithDuration(2, animations: { () -> Void in
            label.transform = CGAffineTransformMakeTranslation(0, labelHeight)
            }) { ( _) -> Void in
                label.removeFromSuperview()
                
        }
    }
    @objc private func reloadData(){
        getData()
        
    }
    func abc(){
        refreshControl?.endRefreshing()
    }
    private func getData(){
        
//        SVProgressHUD.show()
        
        let since_id = viewModel.list.first?.model?.id ?? 0
        
//        viewModel.getData({ () -> () in
        viewModel.getData(since_id,success:{ (count) -> () in
            SVProgressHUD.dismiss()

            self.tableView.reloadData()
            
            self.refreshControl?.endRefreshing()
            
            self.showCount(count)
            }) { () -> () in
                SVProgressHUD.dismiss()
                self.refreshControl?.endRefreshing()

        }
//
        
    }
}

extension SinaHomeViewController    {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.list.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(SinaHomeReuserIdentifie, forIndexPath: indexPath) as! SinaHomeTableViewCell
        
        
        cell.statusViewModel = viewModel.list[indexPath.row]
        
//        cell.textLabel?.text = viewModel.list[indexPath.row].user?.screen_name
        return cell
    }
}
 